<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let p = new Promise((resolve, reject) => {
            let count = Math.floor(Math.random() * 10);

            setTimeout(() => {
                console.log(count);
                if (count % 2 === 0) {
                    resolve('success');
                } else {
                    reject('I fail')
                }
            })
        })

        // 失败处理
        //一 
        /* p.then(
            (res) => {
                console.log(`我${res}`)
            },
            (err) => {
                console.log(err)
            }
        ) */

        /*         p.then(
                    (res) => {
                        console.log(`${res}`)
                    },
                    (err) => {
                        console.log(err)
                    }
                )
        
                p.then(
                    (res) => {
                        console.log(`${res}`)
                    },
                    (err) => {
                        console.log(err)
                    }
                ) */


        // 二
        /*  p.then((res) => {
             console.log(res)
         }).catch((err) => {
             console.log('catch: ' + err)
         })
 
         p.then((res) => {
             console.log(res)
         }).catch((err) => {
             console.log(`catch: ${err}`)
         })
 
         p.then((res) => {
             console.log(res)
         }).catch((err) => {
             console.log(`catch: ${err}`)
         }) */

        // 三
        try {
            p.then(res => {
                console.log(res)
            })
        } catch (err) {
            console.log(err)
        }

        /*       try {
                  p.then(res => {
                      console.log(res)
                  })
              } catch (err) {
                  console.log(err)
              }
      
              try {
                  p.then(res => {
                      console.log(res)
                  })
              } catch (err) {
                  console.log(err)
              } */

        setTimeout(() => {
            console.log('我是2秒后执行');
        }, 2000)
    </script>
</body>

</html>